﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace MetaDemo.Tabs
{
    public partial class ObjectIntroduction : UserControl
    {

        private Assembly assemblyOfObject = null;
        public Assembly AssemblyOfObject { get { return assemblyOfObject; } set { assemblyOfObject = value; } }

        private Type typeOfObject = null;
        public Type TypeOfObject { get { return typeOfObject; } set { typeOfObject = value; } }

        public static TabPage Get(Object o)
        {
            // Display information on the object.
            var introduction = new ObjectIntroduction();

            introduction.TypeOfObject = o.GetType();
            introduction.linkClassnameValue.Text = o.GetType().FullName;
            introduction.linkClassnameValue.LinkClicked += new LinkLabelLinkClickedEventHandler(DisplayClassInformation);
            introduction.lblNamespaceValue.Text = o.GetType().Namespace;
            introduction.lblAttributesValue.Text = o.GetType().Attributes.ToString();

            // Get the assembly.
            var assembly = Assembly.GetAssembly(o.GetType());
            introduction.AssemblyOfObject = assembly;
            introduction.linkAssemblyValue.Text = assembly.GetName().Name;
            introduction.linkAssemblyValue.LinkClicked += new LinkLabelLinkClickedEventHandler(DisplayAssemblyInformation);

            // Create a tab page in which to display the information above.
            var page = GetTabPage();
            page.Controls.Add(introduction);

            return page;
        }

        public static TabPage GetTabPage()
        {
            var page = new TabPage();

            page.Location = new System.Drawing.Point(4, 22);
            page.Name = "tabOverview";
            page.Padding = new System.Windows.Forms.Padding(3);
            page.Size = new System.Drawing.Size(428, 391);
            page.Text = "Overview";
            //            page.TabIndex = 0;
            page.UseVisualStyleBackColor = true;
            
            return page;
        }


        private ObjectIntroduction()
        {
            InitializeComponent();
        }



        #region Handler

        static void DisplayAssemblyInformation(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var introduction = (ObjectIntroduction)UIHelper.FindParentWithType((LinkLabel)sender, typeof(ObjectIntroduction));

            if (null == introduction)
                return;

            Forms.ObjectInfoForm.Get(introduction.AssemblyOfObject).Show();
        }

        static void DisplayClassInformation(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var introduction = (ObjectIntroduction)UIHelper.FindParentWithType((LinkLabel)sender, typeof(ObjectIntroduction));

            if (null == introduction)
                return;

            Forms.ObjectInfoForm.Get(introduction.TypeOfObject).Show();
        }

        #endregion

    }
}
